Icaro - Compressor

  • Created by Andrés Segura Tinoco
  • Created on June 17, 2019
In [1]:
# Load Python libraries
import numpy as np
from PIL import Image
In [2]:
# Load Plot libraries
import seaborn as sns
import matplotlib.pyplot as plt
In [3]:
# Loading example image
file_path = "../data/img/example-1.png"
img = Image.open(file_path)

# Show dimensions (resolution)
img.size
Out[3]:
(1920, 1080)
In [4]:
# Show image
img
Out[4]:
In [5]:
# Read file in low level (bit list)
with open(file_path, 'rb') as f:
    low_bit_list = [byte & 1 for byte in bytearray(f.read())]
In [6]:
# Show size (KB)
round(len(low_bit_list) / 1024, 2)
Out[6]:
2728.96
In [7]:
# Show size (MB)
round(len(low_bit_list) / 1024 / 1020, 2)
Out[7]:
2.68
In [8]:
# Create a matrix
row_len = 2232
col_len = 1252
matrix = np.zeros((row_len, col_len))
matrix.shape
Out[8]:
(2232, 1252)
In [9]:
# Calculate additional bits
gap = np.prod(matrix.shape) - len(low_bit_list)
gap
Out[9]:
6
In [10]:
# Save bits into matrix
data = np.array(low_bit_list)
for i in range(0, len(data)):
    ix_row = int(i / col_len)
    ix_col = i % col_len
    matrix[ix_row][ix_col] = data[i]
In [11]:
# Plot image in binary
fig, ax = plt.subplots(figsize = (14, 14))
sns.heatmap(matrix, ax = ax)
ax.set_title("Image in Binary", fontsize = 16)
ax.set_xlabel('columns', fontsize = 12)
ax.set_ylabel('rows', fontsize = 12)
plt.show()